package com.example.x6.configurationmaintenance.Activity;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.SwitchPreference;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;
import com.example.x6.configurationmaintenance.Interface.LogPrecess.OnLogProcessChangeListener;
import com.example.x6.configurationmaintenance.Module.LogOperationManager;
import com.example.x6.configurationmaintenance.R;
import java.io.File;
import java.text.SimpleDateFormat;

public class LogExportActivity extends PreferenceActivity implements OnLogProcessChangeListener {
    private final int CONTINUAL_LOG = 3;
    private final int CONTINUAL_LOG_PACK = 4;
    private final String TAG = getClass().getSimpleName();
    private final int TEMP_LOG = 1;
    private final int TEMP_LOG_PACK = 2;
    private Context context;
    private PreferenceCategory continualLog;
    private final String continualLogDir = "/sdcard/MC_CONFIG_LOG/continualLog/";
    private Preference continualLogFileClear;
    private Preference continualLogFilePackClear;
    private Preference continualLogPack;
    private final String continualLogPackDir = "/sdcard/MC_CONFIG_LOG/continualLogPack/";
    private SwitchPreference continualLogRecord;
    private LogOperationManager logOperationManager;
    private PreferenceCategory menuTitle;
    private ProgressDialog progressDialog;
    private PreferenceCategory tempLog;
    private final String tempLogDir = "/sdcard/MC_CONFIG_LOG/tempLog/";
    private Preference tempLogFileClear;
    private Preference tempLogFilePackClear;
    private Preference tempLogPack;
    private final String tempLogPackDir = "/sdcard/MC_CONFIG_LOG/tempLogPack/";
    private Preference tempLogRecord;

    /* access modifiers changed from: protected */
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.log_export);
        this.context = this;
        initDir();
        LogOperationManager.initializeInstance(this);
        this.logOperationManager = LogOperationManager.getInstance();
        this.logOperationManager.setOnLogProcessChangeListener(this);
        initView();
        initEvent();
    }

    private void initView() {
        this.progressDialog = new ProgressDialog(this);
        this.progressDialog.setCancelable(false);
        this.progressDialog.setTitle(getResources().getString(R.string.tips));
        this.tempLogRecord = findPreference(getResources().getString(R.string.temp_log_record));
        this.tempLogPack = findPreference(getResources().getString(R.string.temp_log_file_pack));
        this.tempLogFileClear = findPreference(getResources().getString(R.string.temp_log_file_clear));
        this.tempLogFilePackClear = findPreference(getResources().getString(R.string.temp_log_file_pack_clear));
        this.continualLogRecord = (SwitchPreference) findPreference(getResources().getString(R.string.continual_log_record));
        this.continualLogPack = findPreference(getResources().getString(R.string.continual_log_file_pack));
        this.continualLogFileClear = findPreference(getResources().getString(R.string.continual_log_file_clear));
        this.continualLogFilePackClear = findPreference(getResources().getString(R.string.continual_log_file_pack_clear));
        if (this.logOperationManager.isEnableBackgroundProcess()) {
            this.continualLogRecord.setChecked(true);
        } else {
            this.continualLogRecord.setChecked(false);
        }
    }

    private void initEvent() {
        this.tempLogPack.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            /* class com.example.x6.configurationmaintenance.Activity.LogExportActivity.AnonymousClass1 */

            public boolean onPreferenceClick(Preference preference) {
                LogExportActivity.this.showProgressDialog("正在打包...");
                LogExportActivity.this.logOperationManager.startPackTempLog();
                return true;
            }
        });
        this.tempLogFileClear.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            /* class com.example.x6.configurationmaintenance.Activity.LogExportActivity.AnonymousClass2 */

            public boolean onPreferenceClick(Preference preference) {
                LogExportActivity.this.clearLog(1);
                return true;
            }
        });
        this.tempLogFilePackClear.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            /* class com.example.x6.configurationmaintenance.Activity.LogExportActivity.AnonymousClass3 */

            public boolean onPreferenceClick(Preference preference) {
                LogExportActivity.this.clearLog(2);
                return true;
            }
        });
        this.continualLogPack.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            /* class com.example.x6.configurationmaintenance.Activity.LogExportActivity.AnonymousClass4 */

            public boolean onPreferenceClick(Preference preference) {
                LogExportActivity.this.showProgressDialog(LogExportActivity.this.getResources().getString(R.string.packing));
                LogExportActivity.this.logOperationManager.startPackContinualLog();
                return true;
            }
        });
        this.continualLogFileClear.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            /* class com.example.x6.configurationmaintenance.Activity.LogExportActivity.AnonymousClass5 */

            public boolean onPreferenceClick(Preference preference) {
                if (!LogExportActivity.this.logOperationManager.isEnableBackgroundProcess()) {
                    LogExportActivity.this.clearLog(3);
                    return true;
                }
                LogExportActivity.this.Toast(LogExportActivity.this.getResources().getString(R.string.close_continual_log_first));
                return true;
            }
        });
        this.continualLogFilePackClear.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            /* class com.example.x6.configurationmaintenance.Activity.LogExportActivity.AnonymousClass6 */

            public boolean onPreferenceClick(Preference preference) {
                LogExportActivity.this.clearLog(4);
                return true;
            }
        });
        this.continualLogRecord.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            /* class com.example.x6.configurationmaintenance.Activity.LogExportActivity.AnonymousClass7 */

            public boolean onPreferenceChange(Preference preference, Object o) {
                if (LogExportActivity.this.continualLogRecord.isChecked()) {
                    Log.w(LogExportActivity.this.TAG, "停止服务");
                    LogExportActivity.this.logOperationManager.stopBackgroundLogProcess();
                    return true;
                }
                Log.w(LogExportActivity.this.TAG, "启动服务");
                LogExportActivity.this.logOperationManager.startBackgroundLogProcess("/sdcard/MC_CONFIG_LOG/continualLog/", new SimpleDateFormat("MMdd_HHmmss").format(Long.valueOf(System.currentTimeMillis())));
                return true;
            }
        });
        this.tempLogRecord.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            /* class com.example.x6.configurationmaintenance.Activity.LogExportActivity.AnonymousClass8 */

            public boolean onPreferenceClick(Preference preference) {
                LogExportActivity.this.showProgressDialog(LogExportActivity.this.getResources().getString(R.string.geting_log));
                LogExportActivity.this.logOperationManager.startFrontLogProcess("/sdcard/MC_CONFIG_LOG/tempLog/", new SimpleDateFormat("MMdd_HHmmss").format(Long.valueOf(System.currentTimeMillis())));
                return true;
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void clearLog(final int type) {
        runOnUiThread(new Runnable() {
            /* class com.example.x6.configurationmaintenance.Activity.LogExportActivity.AnonymousClass9 */

            public void run() {
                new AlertDialog.Builder(LogExportActivity.this.context).setMessage(LogExportActivity.this.getResources().getString(R.string.clean_log_tips)).setTitle(LogExportActivity.this.getResources().getString(R.string.warning)).setPositiveButton(LogExportActivity.this.getResources().getString(R.string.yes), new DialogInterface.OnClickListener() {
                    /* class com.example.x6.configurationmaintenance.Activity.LogExportActivity.AnonymousClass9.AnonymousClass2 */

                    public void onClick(DialogInterface dialogInterface, int i) {
                        LogExportActivity.this.showProgressDialog(LogExportActivity.this.getResources().getString(R.string.cleaning));
                        switch (type) {
                            case 1:
                                LogExportActivity.this.logOperationManager.startClearTempLog();
                                return;
                            case 2:
                                LogExportActivity.this.logOperationManager.startClearTempLogPack();
                                return;
                            case 3:
                                LogExportActivity.this.logOperationManager.startClearContinualLog();
                                return;
                            case 4:
                                LogExportActivity.this.logOperationManager.startClearContinualLgoPack();
                                return;
                            default:
                                LogExportActivity.this.dissProgressDialog();
                                return;
                        }
                    }
                }).setNegativeButton(LogExportActivity.this.getResources().getString(R.string.no), new DialogInterface.OnClickListener() {
                    /* class com.example.x6.configurationmaintenance.Activity.LogExportActivity.AnonymousClass9.AnonymousClass1 */

                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                }).show();
            }
        });
    }

    private void initDir() {
        File nonblock = new File("/sdcard/MC_CONFIG_LOG/tempLog/");
        File block = new File("/sdcard/MC_CONFIG_LOG/continualLog/");
        File nonBlockPack = new File("/sdcard/MC_CONFIG_LOG/tempLogPack/");
        File blockPack = new File("/sdcard/MC_CONFIG_LOG/continualLogPack/");
        if (!nonblock.exists()) {
            nonblock.mkdirs();
        }
        if (!block.exists()) {
            block.mkdirs();
        }
        if (!nonBlockPack.exists()) {
            nonBlockPack.mkdirs();
        }
        if (!blockPack.exists()) {
            blockPack.mkdirs();
        }
    }

    @Override // com.example.x6.configurationmaintenance.Interface.LogPrecess.OnLogProcessChangeListener
    public void onLogBackgroundProcessFinished(boolean isSucessful) {
    }

    @Override // com.example.x6.configurationmaintenance.Interface.LogPrecess.OnLogProcessChangeListener
    public void onLogFrontProcessFinished(boolean isSucessful, String[] filePath) {
        Resources resources;
        int i;
        dissProgressDialog();
        if (isSucessful) {
            resources = getResources();
            i = R.string.get_log_sucessful;
        } else {
            resources = getResources();
            i = R.string.get_log_failed;
        }
        Toast(resources.getString(i));
    }

    @Override // com.example.x6.configurationmaintenance.Interface.LogPrecess.OnLogProcessChangeListener
    public void onTempLogPackFinished(String path) {
        if (path != null) {
            Toast(getResources().getString(R.string.pack_sucessful_show_dir) + path);
        } else {
            Toast(getResources().getString(R.string.pack_failed));
        }
        dissProgressDialog();
    }

    @Override // com.example.x6.configurationmaintenance.Interface.LogPrecess.OnLogProcessChangeListener
    public void onContinualLogPackFinished(String path) {
        if (path != null) {
            Toast(getResources().getString(R.string.pack_sucessful_show_dir) + path);
        } else {
            Toast(getResources().getString(R.string.pack_failed));
        }
        dissProgressDialog();
    }

    @Override // com.example.x6.configurationmaintenance.Interface.LogPrecess.OnLogProcessChangeListener
    public void onTempLogClearFinished(boolean isSucessful) {
        if (isSucessful) {
            Toast(getResources().getString(R.string.operation_success));
        } else {
            Toast(getResources().getString(R.string.operation_failed));
        }
        dissProgressDialog();
    }

    @Override // com.example.x6.configurationmaintenance.Interface.LogPrecess.OnLogProcessChangeListener
    public void onTempLogPackClearFinished(boolean isSucessful) {
        if (isSucessful) {
            Toast(getResources().getString(R.string.operation_success));
        } else {
            Toast(getResources().getString(R.string.operation_failed));
        }
        dissProgressDialog();
    }

    @Override // com.example.x6.configurationmaintenance.Interface.LogPrecess.OnLogProcessChangeListener
    public void onContinualLogClearFinished(boolean isSucessful) {
        if (isSucessful) {
            Toast(getResources().getString(R.string.operation_success));
        } else {
            Toast(getResources().getString(R.string.operation_failed));
        }
        dissProgressDialog();
    }

    @Override // com.example.x6.configurationmaintenance.Interface.LogPrecess.OnLogProcessChangeListener
    public void onContinualLogPackClearFinished(boolean isSucessful) {
        if (isSucessful) {
            Toast(getResources().getString(R.string.operation_success));
        } else {
            Toast(getResources().getString(R.string.operation_failed));
        }
        dissProgressDialog();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void Toast(final String message) {
        if (message != null) {
            runOnUiThread(new Runnable() {
                /* class com.example.x6.configurationmaintenance.Activity.LogExportActivity.AnonymousClass10 */

                public void run() {
                    Toast.makeText(LogExportActivity.this.context, message, 1).show();
                }
            });
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void showProgressDialog(String message) {
        if (message == null) {
            message = getResources().getString(R.string.operationing);
        }
        String finalMessage = message;
        runOnUiThread(new Runnable() {
            /* class com.example.x6.configurationmaintenance.Activity.LogExportActivity.AnonymousClass11 */

            public void run() {
                if (LogExportActivity.this.progressDialog != null) {
                    LogExportActivity.this.progressDialog.setMessage(finalMessage);
                    LogExportActivity.this.progressDialog.show();
                }
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void dissProgressDialog() {
        runOnUiThread(new Runnable() {
            /* class com.example.x6.configurationmaintenance.Activity.LogExportActivity.AnonymousClass12 */

            public void run() {
                if (LogExportActivity.this.progressDialog != null) {
                    LogExportActivity.this.progressDialog.dismiss();
                }
            }
        });
    }
}
